/* Simple C program that connects to MySQL Database server*/   
#include   <stdio.h>  
#include   <stdlib.h>  
#include   <string.h>   
#include   <mysql/mysql.h> 
#include   <time.h> 

#include   "route_coverge.h"

Route_link g_HeadArray[PORT_NUMBER][PORT_LISTNUM] = {{ NULL } };
Route_link g_TailArray[PORT_NUMBER][PORT_LISTNUM] = {{ NULL } };
Route_link g_ListArray[PORT_NUMBER] = {  NULL  };


int main( ) {   

MYSQL *conn;  
MYSQL_RES *res;   
MYSQL_ROW row;   
char *server = "127.0.0.1";   
char *user = "root";   
char *password = ""; 
char *database = "vianet_pass";  
char *str = NULL;
conn = mysql_init(NULL);
CLOCK_T start    = 0;
CLOCK_T finish   = 0;
UINT32 uiAddress = 0;
UINT32 uiTotal   = 0;
UINT32 uiMaskLen = 0;
UINT32 uiOutPort = 0;
UINT32 uiListNum = 0;
UINT32 uiRet  = 0xFFFFFFFF;

/* init a link table  include 5 port*/  
Route_link T[PORT_LISTNUM] = { NULL } ;  //  just for tables loop 
Route_link Del  = NULL ;



/* Connect to database */   
if (!mysql_real_connect(conn, server,  user, password, database, 0, NULL, 0))
{  
	fprintf(stderr, "%s\n", mysql_error(conn));   
	exit(1);   
}  
		
	start = clock();
	for( uiOutPort = 0; uiOutPort < PORT_NUMBER ; uiOutPort++ )
	{
		/* get the ip address and sort to diff tables */  
		/* send SQL query */ 
		
		/* 此处要做成可选的，传进来端口*/ 	
		/* 此处可以考虑多线程做*/ 
		#if 0
		if(uiOutPort == 0)
		{
			str = "select ip from vianet_pass_a where  port_channel='A';" ;
		}
		else if( uiOutPort == 1 )
		{
			str = "select ip from vianet_pass_a where  port_channel='B';" ;
		}
		else if( uiOutPort == 2 )
		{
			str = "select ip from vianet_pass_a where  port_channel='C';" ;
		}
		if ( mysql_query(conn, str) ) 
		#endif
		
		//if (mysql_query(conn, "select ip from acceleration where id>2869000 and id<3584438;")) 
		if (mysql_query(conn, "select ip from acceleration where id>2869000 and id<2869201;")) 
		{  
			fprintf(stderr, "%s\n", mysql_error(conn));   
			exit(1);   
		}  
		res = mysql_use_result(conn);   
		/* output table name */   
		printf("MySQL Tables in mysql database:\n"); 
		uiTotal = 0 ;
		uiListNum = 0 ;
		while ( ( row = mysql_fetch_row(res) ) != NULL )   
		{	
			/* get the ip address and sort to diff tables */  
			str = row[0];
			uiAddress = Str_to_Num(str); 
			
			uiRet = Insert_addr( &g_HeadArray[uiOutPort][uiListNum], &g_TailArray[uiOutPort][uiListNum],uiAddress );
			
			uiTotal++;
			if( uiTotal%EVERY_LISTNUM == 0 )
			{
				if(uiTotal%PORT_LISTNUM ==0)
				{
					uiOutPort++;
					uiListNum = 0;
					continue;
				}
				uiListNum++;
				printf("I have insert   %d   tables\n",uiTotal);
			}
		}
		finish = clock();
		printf("I have insert %d tables,use %f seconds\n",uiTotal,(double)(finish - start) / CLOCK_PER_SEC );

	}//end of uiOutPort
	
		
	#if 0
	/*print the list after insert*/
	uiListNum = 0 ;
	printf("After insert ,the new list is:\n");
	for( uiOutPort = 0; uiOutPort < PORT_NUMBER && g_HeadArray[uiOutPort][uiListNum] != NULL; uiOutPort++ )
	{
		printf("The %d list member is : \n",uiOutPort);
		for(uiListNum=0;g_HeadArray[uiOutPort][uiListNum]!= NULL;uiListNum++)
		{
			T[uiListNum] = &(*g_HeadArray[uiOutPort][uiListNum]);
			printf("The %dth tiao lianbiao  member is : \n",uiListNum);
			while( T[uiListNum] != NULL )
			{
				/* 依次打印当前链表值 */   
				printf("%d.",(T[uiListNum]->addr&0xFF000000)>>24);
				printf("%d.",(T[uiListNum]->addr&0xFF0000)>>16);
				printf("%d.",(T[uiListNum]->addr&0xFF00)>>8);
				printf("%d\n",T[uiListNum]->addr&0xFF);
				T[uiListNum]= T[uiListNum]->next ;
			}
		}
		uiListNum = 0;
	}
	/*end of print the list after insert*/
	
	
	/*put the big list into the last node list*/

	
	#endif
	Cover_listJoint();
	#if 0
	/*print the list after paixu */
	
	for( uiOutPort = 0,uiListNum=0; uiOutPort < PORT_NUMBER ; uiOutPort++ )
	{
		printf("after paixu the new uiOutPort %d  list is \n",uiOutPort);
		T[uiListNum] = &(*g_ListArray[uiOutPort]);
		
		while( T[uiListNum] != NULL )
		{
			
			/* 依次打印当前链表值 */   
			printf("%d.",(T[uiListNum]->addr&0xFF000000)>>24);
			printf("%d.",(T[uiListNum]->addr&0xFF0000)>>16);
			printf("%d.",(T[uiListNum]->addr&0xFF00)>>8);
			printf("%d\n",T[uiListNum]->addr&0xFF);
			T[uiListNum]= T[uiListNum]->next ;
		}
		
	}
	#endif
	/*put the big list into the last node list*/
	start = clock();
	for( uiOutPort = 0; uiOutPort < PORT_NUMBER ; uiOutPort++ )
	{
		if(g_ListArray[uiOutPort] == NULL )
		{
			continue ;
		}
			
		uiRet = Coverge_addr( &g_ListArray[uiOutPort],uiOutPort); 
		
	}
	finish = clock();
	printf("I have use %f seconds to coverge\n",(double)(finish - start) / CLOCK_PER_SEC);
	printf("After Coverge ,the new list is:\n");
	for( uiOutPort = 0,uiListNum=0; uiOutPort < 1 ; uiOutPort++ )
	{
		T[uiListNum] = &(*g_ListArray[uiOutPort]);
		uiTotal = 0;
		T[uiListNum] = T[uiListNum]->next ;
		while(T[uiListNum] != NULL)
		{
			printf("IP route-static ");
			/* 依次打印当前链表值 */   
			printf("%3d.",(T[uiListNum]->addr&0xFF000000)>>24);
			printf("%3d.",(T[uiListNum]->addr&0xFF0000)>>16);
			printf("%3d.",(T[uiListNum]->addr&0xFF00)>>8);
			printf("%3d",T[uiListNum]->addr&0xFF);
			
			
			uiMaskLen = 0;
			while( ( T[uiListNum]->addr << uiMaskLen ) != 0x80000000 && ( T[uiListNum]->addr << uiMaskLen ) != 0x0)
			{
				uiMaskLen++;
			}
			if(uiOutPort == 0)
			{
				printf("   mask  %2d next-hop A \n",++uiMaskLen );
			}
			else if(uiOutPort == 1){
				printf("   mask  %2d next-hop B \n",++uiMaskLen );
			}
			else if(uiOutPort == 2)
			{
				printf("   mask  %2d next-hop C \n",++uiMaskLen );
			}
			
			Del = T[uiListNum] ;
			T[uiListNum] = T[uiListNum]->next ;
			free( Del );
			//Del = NULL ;
			uiTotal++;
		}
		
	}
		printf("After Coverge ,the total table is:%d\n",uiTotal);
		
		/* close connection */   
		mysql_free_result(res);   
		mysql_close(conn); 
		mysql_library_end();
		
		return OK;
}   


